#include "Common.h"
#include "RenderWindow.h"
#include "Camera.h"
#include "WindowsMessage.h"
#include "BackBuffer.h"

//------------------------------------------
RenderWindow::RenderWindow(void)
	: mCamera(NULL)
	, mWidth(DEFAULT_WIDTH)
	, mHeight(DEFAULT_HEIGHT)
	, mBackBuffer(NULL)
{
}
//------------------------------------------
RenderWindow::~RenderWindow(void)
{
	SAFE_DELETE(mBackBuffer);

	Exit();
}
//------------------------------------------
bool RenderWindow::Init()
{
    // Register the window class
    WNDCLASSEX wc = {
        sizeof( WNDCLASSEX ), CS_CLASSDC, WindowsMessage::MsgProc, 0L, 0L,
        GetModuleHandle( NULL ), 
		LoadIcon(0, IDI_APPLICATION), 
		LoadCursor(NULL, IDC_ARROW), NULL, NULL,
        TITLE, NULL};
    RegisterClassEx(&wc);

    // Create the application's window
    mWnd = CreateWindow(TITLE, TITLE,
		WS_OVERLAPPEDWINDOW, 200, 100, mWidth, mHeight,
		NULL, NULL, wc.hInstance, NULL );

	mInstance = wc.hInstance;

    // Show the window
    ShowWindow(mWnd, SW_SHOWDEFAULT);
    UpdateWindow(mWnd);
	

	return true;
}
//------------------------------------------
void RenderWindow::Exit()
{
	if (mInstance != NULL)
		UnregisterClass(TITLE, mInstance);
	mInstance = NULL;
}
//------------------------------------------
void RenderWindow::Update()
{
	if (mCamera != NULL)
		mCamera->Render(mBackBuffer);
}
//------------------------------------------
void RenderWindow::OnRenderInited()
{
	mBackBuffer = new BackBuffer();
	mBackBuffer->Init();
}
//------------------------------------------
void RenderWindow::OnWindowResized(int widht, int height)
{
	if (widht > 0) mWidth = widht;
	if (height > 0) mHeight = height;

	if (mBackBuffer)
		mBackBuffer->Init();
}
//------------------------------------------